package com.api.gbhg.lbj.action;

import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.web.PropertyManagementAction;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 人员经费额度提交冻结 YS29-YS33
 * @date 2021/4/30 9:56
 */
public class UserQuotadetailFrozenAction extends BaseBean implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入明细预算项目使用单位冻结金额UserQuotadetailAction---");
        ReimbursementDao rei =new ReimbursementDao();
        //请求id
        String requestid = requestInfo.getRequestid();
        //标题
        String description = requestInfo.getDescription();
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        //流程id
        String workflowid = requestInfo.getWorkflowid();
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        rs.next();
        String mainid = rs.getString("id");
        String userFudsWorkflowid29 = getPropValue("ReimbursementWorkflowid", "YS29Workflowid");//读取配置文件
        String userFudsWorkflowid30 = getPropValue("ReimbursementWorkflowid", "YS30Workflowid");//读取配置文件
        String userFudsWorkflowid31 = getPropValue("ReimbursementWorkflowid", "YS31Workflowid");//读取配置文件
        String userFudsWorkflowid32 = getPropValue("ReimbursementWorkflowid", "YS32Workflowid");//读取配置文件
        String userFudsWorkflowid33 = getPropValue("ReimbursementWorkflowid", "YS33Workflowid");//读取配置文件
        String ysbzhpr = getPropValue("ReimbursementWorkflowid", "ysbzhpr");//读取配置文件
        String userFudsactiveWFID29 = WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid29);
        String userFudsactiveWFID30 = WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid30);
        String userFudsactiveWFID31 = WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid31);
        String userFudsactiveWFID32 = WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid32);
        String userFudsactiveWFID33 = WorkflowVersion.getActiveVersionWFID(userFudsWorkflowid33);
        logger.info("mainid:"+mainid+"workflow29:"+userFudsactiveWFID29+"workflow30:"+userFudsactiveWFID30+"ysbzhpr:"+ysbzhpr);
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[0].getTableDBName(), mainid);
        Map<String,String>[] list=new HashMap[recordSet.getArray().size()];
        int i=0;
        while (recordSet.next()){
            list[i] = new HashMap<>();
            String ysxm = recordSet.getString("ysxm");
            String sqbxje = recordSet.getString("sqbxje");
            String bmjjzcfl = recordSet.getString("bmjjzcfl");
            String zcqd = recordSet.getString("zcqd");
            String ysly = recordSet.getString("ysly");
            String fzysrfl = recordSet.getString("fzysrfl");
            String xzkmdm = recordSet.getString("xzkmdm");
            String nd = recordSet.getString("nd");
            String zcdfczbkxm = recordSet.getString("zcdfczbkxm");
            String cxje = recordSet.getString("cxje");
            String tybxje = recordSet.getString("tybxje");
            String dw = rs.getString("szdlhsdw");
            if (StringUtils.isBlank(tybxje)){
                tybxje="0.00";
            }
            if (StringUtils.isBlank(cxje)){
                cxje="0.00";
            }
            logger.info("预算项目:" + ysxm + "申请报销金额:" + sqbxje +"同意报销金额:"+tybxje+",冲销金额:"+cxje+ ",部门经济支出分类:" + bmjjzcfl + "支出渠道:" + zcqd + "预算来源:" + ysly + "非中央收入分类:" + fzysrfl);
            list[i].put("lcjlid", ysxm);//预算项目
            list[i].put("money", sqbxje);//申请报销金额
            list[i].put("bmjjzcfl", bmjjzcfl);//部门经济支出分类
            list[i].put("zcqd", zcqd);//支出渠道
            list[i].put("ysly", ysly);//预算来源
            list[i].put("fzysrfl", fzysrfl);//非中央收入分类
            list[i].put("xzkmdm", xzkmdm);//选择科目代码
            list[i].put("nd", nd);//选择科目代码
            list[i].put("zcdfczbkxm", zcdfczbkxm);//支出地方财政拨款项目
            list[i].put("cxje", cxje);//支出地方财政拨款项目
            list[i].put("tybxje", tybxje);//同意报销金额
            list[i].put("dw", dw);//单位
            list[i].put("userFudsactiveWFID29", userFudsactiveWFID29);
            list[i].put("userFudsactiveWFID30", userFudsactiveWFID30);
            list[i].put("userFudsactiveWFID31", userFudsactiveWFID31);
            list[i].put("userFudsactiveWFID32", userFudsactiveWFID32);
            list[i].put("userFudsactiveWFID33", userFudsactiveWFID33);
            list[i].put("workflowid", workflowid);
            i++;
        }
        PropertyManagementAction.addDetail(list,description,workflowid);
        boolean b1=false;
        boolean b2=false;
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {
            b1 = rei.queryJingfei(rst,list,false,0);
            b2 = rei.jingfeilaiyuandonjie(rst,list, false);//提交冻结
            logger.info("b1:"+b1+",b2:"+b2);
            if(!b1||!b2){
                rst.rollback();
                //删除报销记录
                rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
                logger.info("=====================冻结错误=======================");
                requestInfo.getRequestManager().setMessagecontent("冻结失败");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
            //删除报销记录
            rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
            rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
            rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
            logger.info("=====================冻结错误=======================");
            requestInfo.getRequestManager().setMessagecontent("冻结失败 error "+e.getMessage());
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }

        rst.commit();
        return Action.SUCCESS;
    }
}
